package com.xjw.coreutil.common;

import com.xjw.coreutil.exception.ServiceException;

import java.io.File;
import java.io.IOException;

public class FileUtil {

    public static File newFile(String path) {
        File file = new File(path);
        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
                throw new ServiceException("父目录创建失败");
            }
        }
        if (!file.exists()) {
            try {
                if (!file.createNewFile()) {
                    throw new ServiceException("文件创建失败");
                }
            } catch (IOException e) {
                throw new ServiceException("文件创建失败", e);
            }
        }
        return file;
    }

    public static File newFile(String... paths) {
        StringBuilder stb = new StringBuilder();
        for (int i = 0;i < paths.length - 1;i++) {
            stb.append(paths[i]).append(File.separator);
        }
        stb.append(paths[paths.length - 1]);
        return newFile(stb.toString());
    }
}
